﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class SampleComboBox : MonoBehaviour {
    private RectTransform rectTrans;
    private Transform contentContainer;
    private CanvasGroup canvasGroup;
    private Button mainBtn;
    private Text mainBtnText;


    private int signal = 0;
    private float alphaRate = 0;

    [SerializeField]
    private int itemHeight = 60;
    [SerializeField]
    private List<string> items = new List<string>();

    // Use this for initialization
    void Awake () {

        rectTrans = GetComponent<RectTransform>();
        contentContainer = transform.Find("Template/Viewport/Content");
        mainBtn = transform.Find("MainButton").GetComponent<Button>();
        mainBtn.onClick.AddListener(OnMainBtnClicked);
        mainBtnText = GetComponentInChildren<Text>();


        canvasGroup = transform.Find("Template").GetComponent<CanvasGroup>();
        canvasGroup.alpha = 0.0f;
        canvasGroup.gameObject.SetActive(false);
        RefreshItems();

        
        
	}
	
    private void CreateNewButton(string buttonName, string itemName)
    {
        GameObject buttonObj = Instantiate(mainBtn.gameObject, contentContainer);
        buttonObj.name = buttonName;
        RectTransform buttonRect = buttonObj.GetComponent<RectTransform>();
        buttonRect.sizeDelta = new Vector2(0, itemHeight);
        buttonObj.GetComponentInChildren<Text>().text = itemName;
        buttonObj.SetActive(true);
        buttonObj.GetComponent<Button>().onClick.AddListener(()=> {
            OnItemBtnClicked(EventSystem.current.currentSelectedGameObject);
        });
    }

    private void OnItemBtnClicked(GameObject clickObj)
    {
        mainBtnText.text = clickObj.GetComponentInChildren<Text>().text;
        OnMainBtnClicked();

        if (onValueChangeAction != null) onValueChangeAction(mainBtnText.text);
    }

    private void OnMainBtnClicked()
    {
        canvasGroup.gameObject.SetActive(true);
        if (signal == 0) signal = 1;
        else signal *= -1;
    }


    private void RefreshItems()
    {
        Button[] btns = contentContainer.GetComponentsInChildren<Button>();
        for (int i = 0; i < btns.Length; i++)
            Destroy(btns[i].gameObject);

        for (int i = 0; i < items.Count; i++)
            CreateNewButton("ItemButtonGroup", items[i]);
    }

    public bool AddItem(string item, int index = -1)
    {
        if (items.Contains(item)) return false;
        
        if(index < 0 || index > items.Count) items.Add(item);
        else items.Insert(index, item);
        
        RefreshItems();
        return true;
    }
    public void ClearItem()
    {
        items.Clear();
        RefreshItems();
    }
    private void Update()
    {
 
        alphaRate += Time.deltaTime * signal * 10;
        if(alphaRate >= 1.0f)
        {

        }
        if(alphaRate <= 0.0f)
        {
            canvasGroup.gameObject.SetActive(false);
        }
        alphaRate = Mathf.Clamp01(alphaRate);
        canvasGroup.alpha = alphaRate;
    }

    public bool SetCurrentItem(string item)
    {
        if (!items.Contains(item)) return false;
        if(mainBtnText == null) mainBtnText = GetComponentInChildren<Text>();
        mainBtnText.text = item;
        return true;
    }
    private Action<string> onValueChangeAction;
    public void OnValueChanged(Action<string> listener)
    {
        onValueChangeAction = listener;
    }

   
}
